<template>
  <div class="create-order-container">
    <div class="page-header">
      <h1>确认订单</h1>
      <p>请确认订单信息并选择支付方式</p>
    </div>
    
    <div class="order-content">
      <el-card shadow="hover" class="order-card">
        <template #header>
          <div class="card-header">
            <span>课程信息</span>
          </div>
        </template>
        
        <div v-if="course" class="course-info">
          <img :src="course.image" :alt="course.name" class="course-image">
          <div class="course-details">
            <h3 class="course-title">{{ course.name }}</h3>
            <p class="course-description">{{ course.description }}</p>
            <div class="course-meta">
              <span class="course-type">{{ courseType }}</span>
              <span class="course-students">{{ course.students }} 人学习</span>
            </div>
          </div>
        </div>
        <div v-else class="loading">
          <el-skeleton :rows="3" animated />
        </div>
      </el-card>
      
      <el-card shadow="hover" class="order-card" style="margin-top: 20px;">
        <template #header>
          <div class="card-header">
            <span>支付方式</span>
          </div>
        </template>
        
        <el-radio-group v-model="selectedPayMethod">
          <el-radio :label="1" class="pay-method-option">
            <div class="pay-method-content">
              <el-icon class="pay-icon wechat-icon"><Message /></el-icon>
              <span>微信支付</span>
            </div>
          </el-radio>
          <el-radio :label="2" class="pay-method-option">
            <div class="pay-method-content">
              <el-icon class="pay-icon alipay-icon"><Wallet /></el-icon>
              <span>支付宝</span>
            </div>
          </el-radio>
        </el-radio-group>
      </el-card>
      
      <el-card shadow="hover" class="order-card" style="margin-top: 20px;">
        <template #header>
          <div class="card-header">
            <span>订单摘要</span>
          </div>
        </template>
        
        <div class="order-summary">
          <div class="summary-item">
            <span>课程价格：</span>
            <span class="price">¥{{ course?.price || 0 }}</span>
          </div>
          <div class="summary-item">
            <span>优惠折扣：</span>
            <span class="discount">-¥0.00</span>
          </div>
          <div class="summary-item total">
            <span>应付金额：</span>
            <span class="total-price">¥{{ course?.price || 0 }}</span>
          </div>
        </div>
      </el-card>
      
      <div class="order-actions">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" size="large" @click="handleSubmitOrder">提交订单</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Wallet, Message } from '@element-plus/icons-vue'
import {createAliPay} from "@/api/Payment.js";
import {coursesById} from "@/api/Course.js";

const route = useRoute()
const router = useRouter()
//课程id
const courseId = route.params.courseId
// 课程数据
const course = ref(null)
// 支付方式
const selectedPayMethod = ref(2) // 默认支付宝


// 计算课程类型文本
const courseType = computed(() => {
  // 根据类型ID返回对应名称
  const typeMap = {
    1: '前端开发',
    2: '后端开发',
    3: '移动开发',
    4: '数据库',
    5: '运维部署'
  }
  return typeMap[course.value?.typeId] || '未知类型'
})

// 加载课程信息
const loadCourseInfo =  () => {
  try {
     //发送异步请求，通过id查询客户曾信息
    coursesById(courseId).then(res=>{
    course.value=res.data;
    })
  } catch (error) {
    console.error('加载课程信息失败:', error)
    ElMessage.error('加载课程信息失败，请稍后重试')
  }
}



// 提交订单
const handleSubmitOrder = async () => {
  try {
    // 显示加载消息
    ElMessage({
      message: '正在创建订单，请稍候...',
      type: 'loading',
      duration: 1500
    })
    //获取用户id
    const user = sessionStorage.getItem("userInfo");
    console.log(user)
    //将user字符串转为json对象
    const userinfo = JSON.parse(user);
    createAliPay({
      userId:userinfo.id,
      courseId:courseId,
      amount:course.value.price,
      subject:course.value.name
    }).then(res=>{
      console.log(res)
        router.push({
          path:'/pay/alipay',
          query:{
            qrCodeUrl:res.qrCodeUrl,
            orderNumber:res.orderNumber,
            courseId:res.courseId,
          }
        })
    })
  } catch (error) {
    console.error('创建订单失败:', error)
    ElMessage.error('创建订单失败，请稍后重试')
  }
}

// 模拟支付成功
const handleSimulatePayment = () => {
  // 模拟支付过程延迟
  ElMessage({
    message: '正在处理支付，请稍候...',
    type: 'loading',
    duration: 2000
  })
  
  // 模拟API调用延迟
  setTimeout(() => {
    // 这里应该调用API确认支付
    ElMessage.success('支付成功！')
    paymentVisible.value = false
    
    // 跳转到学习页面
    router.push(`/course/learn/${courseId}`)
  }, 2000)
}

// 关闭支付对话框
const handleClosePayment = () => {
  paymentVisible.value = false
}

// 取消订单创建
const handleCancel = () => {
  if (courseId) {
    router.push(`/course/detail/${courseId}`)
  } else {
    router.push('/courses')
  }
}

// 页面加载时获取数据
onMounted(() => {
  loadCourseInfo()
})
</script>

<style scoped>
.create-order-container {
  min-height: 100vh;
  background-color: #f5f7fa;
  padding-bottom: 60px;
}

.page-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 30px 0;
  text-align: center;
  margin-bottom: 25px;
}

.page-header h1 {
  font-size: 28px;
  margin-bottom: 8px;
}

.page-header p {
  font-size: 16px;
  opacity: 0.9;
}

.order-content {
  max-width: 700px;
  margin: 0 auto;
  padding: 0 20px;
  position: relative;
}

.order-card {
  background-color: white;
  border-radius: 8px;
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header span {
  font-weight: bold;
  color: #303133;
}

.course-info {
  display: flex;
  gap: 20px;
  padding: 15px 0;
  align-items: center;
}

.course-image {
  width: 160px;
  height: 96px;
  object-fit: cover;
  border-radius: 8px;
  flex-shrink: 0;
}

.course-details {
  flex: 1;
}

.course-title {
  margin: 0 0 8px 0;
  color: #303133;
  font-size: 18px;
  font-weight: 600;
}

.course-description {
  margin: 0 0 15px 0;
  color: #606266;
  font-size: 14px;
  line-height: 1.6;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.course-meta {
  display: flex;
  gap: 20px;
  font-size: 14px;
  color: #909399;
}

.course-type {
  padding: 2px 8px;
  background-color: #ecf5ff;
  color: #667eea;
  border-radius: 4px;
}

.pay-method-option {
  display: flex;
  align-items: center;
  padding: 15px;
  margin-bottom: 10px;
  border: 1px solid #dcdfe6;
  border-radius: 8px;
  transition: all 0.3s;
  cursor: pointer;
}

.pay-method-option:hover {
  border-color: #667eea;
}

.pay-method-option.is-checked {
  border-color: #667eea;
  background-color: #ecf5ff;
}

.pay-method-content {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 16px;
  flex: 1;
}

.pay-icon {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
}

.wechat-icon {
  background-color: #07c160;
}

.alipay-icon {
  background-color: #1677ff;
}

.order-summary {
  padding: 10px 0;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.summary-item:last-child {
  border-bottom: none;
}

.price {
  color: #606266;
}

.discount {
  color: #f56c6c;
}

.total {
  font-size: 16px;
}

.total-price {
  font-size: 20px;
  font-weight: bold;
  color: #f56c6c;
}

.order-actions {
  display: flex;
  justify-content: flex-end;
  gap: 15px;
  margin-top: 25px;
  padding: 20px;
  background-color: white;
  border-radius: 8px;
  position: sticky;
  bottom: 0;
  z-index: 100;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
}

.order-actions .el-button--primary {
  padding: 12px 30px;
}

/* 支付对话框样式 */
.payment-dialog {
  text-align: center;
  padding: 20px 0;
}

.payment-loading {
  padding: 40px 0;
}

.payment-loading p {
  margin-top: 20px;
  color: #606266;
}

.payment-code {
  padding: 20px 0;
}

.payment-code h3 {
  margin-top: 0;
  margin-bottom: 20px;
  color: #303133;
}

.qrcode-placeholder {
  width: 200px;
  height: 200px;
  margin: 0 auto 20px;
  background-color: #f5f7fa;
  border: 1px solid #dcdfe6;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: #909399;
}

.payment-info {
  margin-top: 30px;
  text-align: left;
}

.payment-info p {
  margin: 10px 0;
  color: #606266;
}

.payment-info .amount {
  color: #f56c6c;
  font-weight: bold;
  font-size: 18px;
}

@media (max-width: 768px) {
  .course-info {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }
  
  .course-image {
    width: 100%;
    max-width: 250px;
    height: auto;
  }
  
  .course-meta {
    justify-content: center;
    flex-wrap: wrap;
  }
  
  .order-actions {
    flex-direction: column;
    padding: 15px;
  }
  
  .order-actions .el-button {
    width: 100%;
  }
  
  .order-content {
    padding: 0 15px;
  }
}
</style>